<template>
  <y-dialog @open="open" @close="emit('close')" width="1000px" append-to-body>
    <y-code-editor
      ref="editorRef"
      v-model="fileContent"
      language="python"
      readOnly
    />
  </y-dialog>
</template>

<script setup name="PreView">
import axios from "axios";

const emit = defineEmits(["close"]);

const editorRef = ref(null);
const fileContent = ref("");

const props = defineProps({
  file: { type: String, default: "" },
});

/** 打开对话框事件 */
function open() {
  fetchFileContent();
}

// 获取文件内容
async function fetchFileContent() {
  try {
    const response = await axios.get(
      `${import.meta.env.VITE_APP_BASE_API}/${props.file}`
    );
    fileContent.value = response.data;
  } catch (error) {
    console.error("Failed to load file content:", error);
    alert("无法加载文件内容");
  }
}
</script>
